<template>
    <div class="app-container">
        <el-row :gutter="20">
            <el-col :span="24" :xs="24">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
                    <el-form-item :label="$locale('classList.add.class name')" prop="className">
                        <el-input v-model="queryParams.className" :placeholder="$locale('classList.add.Please enter the class name')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item :label="$locale('geWudevice.add.state')" prop="status">
                        <el-select v-model="queryParams.status" :placeholder="$locale('geWudevice.add.state')" clearable style="width: 240px">
                            <el-option v-for="dict in dict.type.data_status" :key="dict.value" :label="dict.label" :value="+dict.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="$locale('classList.add.Teacher in charge')" prop="teacherName">
                        <el-input v-model="queryParams.teacherName" :placeholder="$locale('classList.Please enter the responsible teacher')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>

                    <el-form-item :label="$locale('geWudevice.Creation time')" prop="createTime">
                        <el-date-picker
                            v-model="dateRange"
                            style="width: 240px"
                            value-format="yyyy-MM-dd"
                            type="daterange"
                            range-separator="-"
                            :start-placeholder="$locale('classList.Start date')"
                            :end-placeholder="$locale('classList.End date')"></el-date-picker>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $locale('geWudevice.add.search for') }}</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $locale('geWudevice.reset') }}</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">{{ $locale('classList.add.added') }}</el-button>
                    </el-col>
                </el-row>

                <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55"></el-table-column>
                    <el-table-column :label="$locale('classList.Class ID')" align="center" key="id" prop="id" />
                    <el-table-column :label="$locale('classList.class')" align="center" key="className" prop="className" />
                    <el-table-column :label="$locale('classList.add.Teacher in charge')" align="center" key="teacherName" prop="teacherName"></el-table-column>
                    <el-table-column
                        sortable
                        column-key="status"
                        :filters="[
                            { text: $locale('classList.normal'), value: '1' },
                            { text: $locale('classList.freeze'), value: '0' }
                        ]"
                        :filter-method="filterHandler"
                        :label="$locale('geWudevice.add.state')"
                        align="center"
                        key="status"
                        prop="status">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.status == 0 ? 'success' : 'danger'" effect="dark">
                                {{ scope.row.status == 0 ? $locale('classList.normal') : $locale('classList.freeze') }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('geWudevice.founder')" align="center" key="createBy" prop="createBy" />
                    <el-table-column :label="$locale('geWudevice.Creation time')" align="center" prop="createTime" width="160"></el-table-column>
                    <el-table-column :label="$locale('formMarkering.list.operate')" align="center" width="250" class-name="small-padding fixed-width">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">{{ $locale('classList.add.modify') }}</el-button>
                            <el-button type="text" size="mini" @click="handleFreeze(scope.row)">{{ scope.row.status == 0 ? $locale('classList.freeze') : $locale('classList.thaw') }}</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
            </el-col>
        </el-row>

        <!-- 添加 -->
        <addDeviceList ref="addDeviceList" @getList="getList"></addDeviceList>
    </div>
</template>

<script>
import { deleteById, list, setStatus } from '@/api/myClass/classList';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
import addDeviceList from './add/index.vue';
import { getNames } from '@/utils/index';

export default {
    name: 'school',
    dicts: ['difficulty_level', 'data_status'],
    components: { Treeselect, addDeviceList },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 日期范围
            dateRange: [],
            // 用户表格数据
            deviceList: [],
            // 弹出层标题
            title: '',
            loginForm: {
                id: undefined,
                password: undefined
            },
            // 部门树选项
            deptOptions: undefined,
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                className: '',
                status: '',
                teacherName: ''
            },
            // 表单校验
            rules: {
                id: [
                    { required: true, message: this.$locale('classList.Please enter your login password'), trigger: 'blur' },
                    { min: 6, max: 20, message: this.$locale('classList.Password length is 6 to 20 digits'), trigger: 'blur' }
                ],
                password: [
                    { required: true, message: this.$locale('classList.Please enter your login password again'), trigger: 'blur' },
                    { validator: this.validatePasswordConfirm, trigger: 'blur' }
                ]
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        validatePasswordConfirm(rule, value, callback) {
            if (value === '') {
                callback(new Error(this.$locale('classList.Please enter your login password again')));
            } else if (value !== this.loginForm.password) {
                callback(new Error(this.$locale('classList.Entered passwords differ')));
            } else {
                callback();
            }
        },
        /** 查询用户列表 */
        getList() {
            this.loading = true;
            if (this.dateRange.length > 0) {
                this.queryParams['startTime'] = this.dateRange[0];
                this.queryParams['endTime'] = this.dateRange[1];
            } else {
                this.queryParams['startTime'] = undefined;
                this.queryParams['endTime'] = undefined;
            }
            list(this.queryParams).then(({ data }) => {
                this.deviceList = data.records;
                this.total = data.total;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        filterHandler(value, row, column) {
            const property = column['property'];
            return row[property] === value;
        },
        /** 重置按钮操作 */
        resetQuery() {
          this.dateRange = []
            this.resetForm('queryForm');
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.$refs.addDeviceList.handleAdd();
        },
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
        },
        /** 冻结按钮操作 */
        handleFreeze(row) {
            const Ids = (row && row.id) || this.ids;
            this.$modal
                .confirm(this.$locale('geWudevice.tip4') + getNames(this.deviceList, Ids, 'deviceName') + this.$locale('geWudevice.tip2'))
                .then(() => {
                    if (row) {
                        setStatus(Ids).then(() => this.getList());
                    }
                })
                .then((res) => {
                    if (res.code == 200) this.$modal.msgSuccess(this.$locale('geWudevice.tip5'));
                })
                .catch(() => {});
        },
        /** 修改按钮操作 */
        handleUpdate(row, disabled) {
            this.$refs.addDeviceList.handleUpdate(row, disabled);
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm(this.$locale('geWudevice.tip1') + getNames(this.deviceList, Ids, 'deviceName') + this.$locale('geWudevice.tip2'))
                .then(function () {
                    return deleteById({ id: ids });
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess(this.$locale('geWudevice.tip3'));
                })
                .catch(() => {});
        }
    }
};
</script>
